package com.goldenautumn.prefabricate.activity.cart.dialog

import android.view.Gravity
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.view.animation.AnimationUtils
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.VibrateUtils
import com.goldenautumn.prefabricate.R
import com.goldenautumn.prefabricate.business.entity.PaymentMethodVo
import com.goldenautumn.prefabricate.business.user.UserAddressUiState
import com.goldenautumn.prefabricate.business.user.UserViewModel
import com.goldenautumn.prefabricate.databinding.DialogPayMethodBinding
import com.goldenautumn.prefabricate.utils.onCenterToast
import kotlinx.coroutines.isActive
import kotlinx.coroutines.selects.select
import qncx.external.common.widget.dialog.BaseDialog
import qncx.external.common.widget.dialog.PromptDialog

class PaymentMethodDialog constructor(var item:PaymentMethodVo?,val block: (PaymentMethodVo?) -> Unit) : BaseDialog<DialogPayMethodBinding>(qncx.external.common.R.style.BottomDialogs) {

    private val mAdapter by lazy {
        PaymentMethodAdapter()
    }

    private val mUserModel by viewModels<UserViewModel>()

    private var selectItem :PaymentMethodVo? = null

    override val layoutRes: Int
        get() = R.layout.dialog_pay_method

    override fun onStart() {
        super.onStart()
        val window: Window? = dialog?.window
        val params: WindowManager.LayoutParams? = window?.attributes
        params?.dimAmount = dimAmount
        params?.width = WindowManager.LayoutParams.MATCH_PARENT
        if (height > 0) params?.height = height
        else params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        params?.gravity = Gravity.BOTTOM
        window?.attributes = params
    }

    override suspend fun bindView(view: View): View {
        mBinding.paymentRv.layoutManager = LinearLayoutManager(context)
        mBinding.paymentRv.adapter = mAdapter
        mBinding.closeBtn.setOnClickListener { dismiss() }
        selectItem = item
        mAdapter.type = item?.value
        mAdapter.notifyDataSetChanged()
        mAdapter.setOnItemClickListener { adapter,_,position ->
            selectItem = adapter.getItem(position)
            mAdapter.type = selectItem?.value
            mAdapter.notifyDataSetChanged()
        }
        mBinding.choose.setOnClickListener {
            if(selectItem == null) {
                VibrateUtils.vibrate(50)
                mBinding.choose.startAnimation(AnimationUtils.loadAnimation(it.context, R.anim.shake_anim))
            } else {
                block(selectItem)
                dismiss()
            }
        }
        mUserModel.onPayType()
        while (isActive) {
            select {
                mUserModel.addressUiState.onReceive {
                    when(it) {
                        is UserAddressUiState.PaymentType -> {
                            mAdapter.submitList(it.payment)
                        }
                        else -> {}
                    }
                }
            }
        }

        return view
    }
}